from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from mg_admin.utils import PageNumPagination
from mg_admin.serializers.order import OrderSerializer
from payment.models import OrderInfo


class OrderView(ModelViewSet):
    pagination_class = PageNumPagination
    queryset = OrderInfo.objects.order_by('-create_time')
    serializer_class = OrderSerializer

    @action(methods=['put'],detail=True)
    def status(self,request,pk):
        # 获取请求参数
        status = request.data.get('status')

        order_obj = self.get_object()
        order_obj.status =status

        order_obj.save()

        # 返回响应数据
        return Response({
            'order_id':order_obj.order_id,
            'status':order_obj.status
        },status=200)